package com.base.component;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

@Component
public class BaseHibernateDAO
{

    @Resource
    private SessionFactory sessionFactory;

    public Session getSession()
    {
        return sessionFactory.getCurrentSession();
    }

    public Serializable save(Object entity)
    {	
        Serializable ser = (Serializable) getSession().save(entity);
        return ser;
    }

    public void update(Object entity)
    {
    	getSession().update(entity);
    }

    public void saveOrUpdate(Object entity)
    {
    	getSession().saveOrUpdate(entity);
    }
    
    public void delete(Object entity)
    {
    	getSession().delete(entity);
    }

    public Object get(Class clazz, Serializable id)
    {
        return getSession().get(clazz, id);
    }

    @SuppressWarnings("unchecked")
    public List query(Class clazz, int offset, int size)
    {
        return getSession().createQuery("from " + clazz.getName()).setFirstResult(offset)
                .setMaxResults(size).list();
    }

    public Long count(Class clazz)
    {
        @SuppressWarnings("unchecked")
        List<Long> list = getSession().createQuery(
                "select count(*) from " + clazz.getName()).list();
        return list.size() == 0 ? 0 : list.get(0);
    }

    public int deleteBatch(Class clazz,String[] pkArray){
    	StringBuffer pkStr = new StringBuffer("0");
    	for (int i = 0; i < pkArray.length; i++) {
			pkStr.append(",").append(pkArray[i]);
		}
    	return getSession().createQuery("delete from " + clazz.getName() + " where id in ("+pkStr.toString()+")").executeUpdate();
    }
    public Long countByProperty(Class clazz, String[] props,Object[] values)
    {
    	Criteria criteria = getSession().createCriteria(clazz);
    	for (int i = 0; i < props.length; i++) {
	    	String param = "%"+values[i]+"%";
	    	criteria.add(Restrictions.like(props[i],param ));
    	}
    	Integer size =  criteria.list().size();
    	return size.longValue();
    }
    
	@SuppressWarnings("unchecked")
	public List findByProperty(Class clazz, String[] props, Object[] values,
			int offset, int pageSize) {
		if (props.length != values.length) {
			throw new IllegalArgumentException("props.length != values.length");
		}
		StringBuffer hql = new StringBuffer("from ").append(clazz.getName())
				.append(" where 1 = 1");
		for (int i = 0; i < props.length; i++) {
			hql.append(" and ").append(props[i]).append(" = ?");
		}
		System.out.println(hql);
		Query query = getSession().createQuery(hql.toString());
		for (int i = 0; i < values.length; i++) {
			query.setParameter(i, values[i]);
		}
		List list = query.setFirstResult(offset).setMaxResults(pageSize).list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List findByHQL(String hql, int offset, int pageSize) {
		Query query = getSession().createQuery(hql);
		List list = query.setFirstResult(offset).setMaxResults(pageSize).list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public Long countByHQL(String hql) {
		Query query = getSession().createQuery(hql);
		List list = query.list();
		return (Long) list.get(0);
	}

    public SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
    
    

}
